﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rep="urn:report-extended-utilities"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl rep">
  <xsl:output method="xml" indent="yes"/>
  <xsl:param name="report-name">SharePoint Generic Report</xsl:param>
  <xsl:param name="report-chart-type">Pie</xsl:param>
  <xsl:param name="report-legend-title">Web Templates</xsl:param>
  <xsl:param name="report-date" />
  <xsl:key name="WebTemp" match="//WebApplications/WebApplication/Sites/Site/Webs/Web" use="@WebTemplate" />
  <xsl:template match="/">
    <xsl:variable name="report-legend-title">Web Templates</xsl:variable>
    <Report Name="{$report-name}" ChartType="{$report-chart-type}" LegendTitle="{$report-legend-title}">
      <Filters>
        <Filter Name="All" FilterAttribute="WebApplication" FilterValue="ALL" />
        <xsl:for-each select="//WebApplications/WebApplication">
          <Filter Name="{@Url}" FilterAttribute="WebApplication" FilterValue="{@Id}" />
        </xsl:for-each>
      </Filters>
      <Series>
        <Serie Name="ALL">
          <xsl:for-each select="//WebApplications/WebApplication/Sites/Site/Webs/Web[generate-id(.)=generate-id(key('WebTemp',@WebTemplate))]">
            <xsl:variable name="WebTemp" select="@WebTemplate" />
              <Item WebApplication="ALL" Name="{$WebTemp}" Date="{$report-date}">
                  <xsl:attribute name="Value">
                  <xsl:value-of select="count(//WebApplications/WebApplication/Sites/Site/Webs/Web[@WebTemplate=$WebTemp])"/>
                </xsl:attribute>
              </Item>
            </xsl:for-each>
        </Serie>
        <xsl:for-each select="//WebApplications/WebApplication">
          <xsl:variable name="WebId" select="@Id"/>
          <Serie Name="{@Url}">
            <xsl:for-each select="Sites/Site/Webs/Web[generate-id(.)=generate-id(key('WebTemp',@WebTemplate))]">
              <xsl:variable name="WebTemp" select="@WebTemplate" />
              <Item Name="{$WebTemp}" WebApplication="{$WebId}" Date="{$report-date}">
                <xsl:attribute name="Value">
                  <xsl:value-of select="count(//WebApplications/WebApplication[@Id=$WebId]/Sites/Site/Webs/Web[@WebTemplate=$WebTemp])"/>
                </xsl:attribute>
              </Item>
            </xsl:for-each>
          </Serie>
        </xsl:for-each>
      </Series>
    </Report>
  </xsl:template>
</xsl:stylesheet>
